//
//  ZAddTableViewCell.m
//  Medicare
//
//  Created by LYY on 2017/8/9.
//  Copyright © 2017年 medicare. All rights reserved.
//

#import "ZAddTableViewCell.h"
#import "ZSchedulingButton.h"
#import "IFMySchedulingPublicDefines.h"
#import "MyTextField.h"
#import "LongPressFactory.h"
#import "POP.h"
#import "ZSchedulingTypeModel.h"

@interface ZAddTableViewCell ()<ZLongPressClickDelegate>

@property (nonatomic, strong)ZSchedulingButton *selectButton;

@end

@implementation ZAddTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.nameTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    UILongPressGestureRecognizer *longGesture = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [self addGestureRecognizer:longGesture];
    [self.nameTextField addTarget:self action:@selector(textFieldChange:) forControlEvents:UIControlEventEditingChanged];
}

- (void)setModel:(ZSchedulingTypeModel *)model {
    _model = model;
    self.nameTextField.userInteractionEnabled = [Common isBlankString:_model.classInfoId];
    for (UIView *view in self.self.contentView.subviews) {
        if (view.tag >= 1000) {
            view.userInteractionEnabled = [Common isBlankString:_model.classInfoId];
        }
    }
    self.nameTextField.text = model.className;
    if (model.color.length > 0 && COLORDIC[model.color]) {
        NSInteger value = model.color.integerValue * 1000;
        ZSchedulingButton *btn = [self viewWithTag:value];
        self.selectButton.selected = NO;
        btn.selected = YES;
        self.selectButton = btn;
        self.nameTextField.textColor = COLORARRAY[btn.tag/1000-1];
    }
}

- (void)textFieldChange:(MyTextField *)sender {
    if (sender.text.length > 10) {
        [SVProgressHUD showTextHUD:@"已经超出长度限制"];
        sender.text = [sender.text substringToIndex:10];
    }
    self.model.className = sender.text;

    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)typeButtonClick:(ZSchedulingButton *)sender {
    
    CGFloat scale = 1.1;
//    sender.selected = !sender.selected;
    if (!sender.isSelected) {
        sender.selected = YES;
        scale = 1.1;
        self.selectButton.selected = NO;
        self.selectButton = sender;
        self.nameTextField.textColor = COLORARRAY[sender.tag/1000-1];
        NSString *tagColor = [NSString stringWithFormat:@"0%ld",sender.tag/1000];
        _model.color = tagColor;
    } else {

    }
    
    POPSpringAnimation *scaleAnimation = [POPSpringAnimation animation];
    scaleAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];//宽高改变
    scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(scale, scale)];//放大
    [sender pop_addAnimation:scaleAnimation forKey:@"scaleAnimationKey"];//执行动画
    scaleAnimation.completionBlock = ^(POPAnimation *animation,BOOL finish) { //动画回调
        POPSpringAnimation *scaleAnimation = [POPSpringAnimation animation];
        scaleAnimation.springBounciness = 16;
        scaleAnimation.springSpeed = 14;
        scaleAnimation.property = [POPAnimatableProperty propertyWithName:kPOPViewScaleXY];
        scaleAnimation.toValue = [NSValue valueWithCGSize:CGSizeMake(1.0, 1.0)];
        [sender pop_addAnimation:scaleAnimation forKey:@"scaleAnimationKey"];
    };
}

-(void)longPress:(UILongPressGestureRecognizer *)longRecognizer
{
    if (longRecognizer.state==UIGestureRecognizerStateBegan) {
        [self becomeFirstResponder];
        if (![self.subviews.lastObject isKindOfClass:[LongPressView class]]) {
            LongPressView *longPress = [[LongPressFactory shareManager] createLongPress];
            longPress.frame =CGRectMake(0, 0, kScreenWidth, CGRectGetHeight(self.frame));
            
           longPress.titleArray = @[@"删除"];
            longPress.delegate = self;
            [self addSubview:longPress];
            [longPress show];
            
        }
    }
}

#pragma mark 处理action事件
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender{
    return [super canPerformAction:action withSender:sender];
}
#pragma mark  实现成为第一响应者方法
- (BOOL)canBecomeFirstResponder{
    return YES;
}

- (void)longPressViewButtonClick:(UIButton *)sender andLongPressView:(LongPressView *)longPress{
    if (sender.tag == 0) {
        if (self.deleteBlock) {
            self.deleteBlock();
        }
    }
}


@end
